import java.io.IOException;
import java.io.*;
import javax.microedition.lcdui.*;

import javax.microedition.midlet.*;
import javax.microedition.rms.*;


import java.util.Enumeration;
import java.util.Hashtable;

import javax.microedition.lcdui.Command;
import javax.microedition.lcdui.CommandListener;
import javax.microedition.lcdui.Display;
import javax.microedition.lcdui.Displayable;
import javax.microedition.lcdui.Form;
import javax.microedition.lcdui.StringItem;
import javax.microedition.lcdui.TextField;

public class Trivia extends MIDlet implements CommandListener
{
	private Display display = Display.getDisplay(this);
	private List list;
	private Image background;
	private Image congrats;
	private Form menu = new Form ("Welcome");
	
	
	private Command start = new Command("start", Command.SCREEN,1);
	private Command select = new Command("Select", Command.SCREEN,1);
	private Command play = new Command ("Select",Command.SCREEN,0);
	private Command submit = new Command ("Submit",Command.SCREEN,0);
	private Command HScore = new Command("View High Scores",Command.SCREEN,0);
	
	private Command Back = new Command ("Back", Command.BACK,0);
	private Command exit = new Command("Exit Game", Command.EXIT, 0);
	private Form question_form, question_form2,question_form3,question_form4,question_form5;
	private Form question_form6, question_form7,question_form8,question_form9,question_form10,Congratulation;
	private Form category,HighScores,f;
	
	private Form game_over;
	private String playername;
	
	private String name[];
	private TextField tempname;
	private int ans,ans2,ans3,ans4,ans5,ans6,ans7,ans8,ans9,ans10;
	private int choice,difficulty;
	private int question_count = 0;
	private int life_count = 4;
	private int score[];
	private int tempscore = 0;
	private ChoiceGroup options = new ChoiceGroup("Select an Option: ", Choice.EXCLUSIVE);
	private ChoiceGroup categories = new ChoiceGroup("Choose a Category: ", Choice.EXCLUSIVE);
	private ChoiceGroup answer = new ChoiceGroup("Choose an answer: ", Choice.EXCLUSIVE);
	private ChoiceGroup answer2 = new ChoiceGroup("Choose an answer: ", Choice.EXCLUSIVE);
	private ChoiceGroup answer3 = new ChoiceGroup("Choose an answer: ", Choice.EXCLUSIVE);
	private ChoiceGroup answer4 = new ChoiceGroup("Choose an answer: ", Choice.EXCLUSIVE);
	private ChoiceGroup answer5 = new ChoiceGroup("Choose an answer: ", Choice.EXCLUSIVE);
	private ChoiceGroup answer6 = new ChoiceGroup("Choose an answer: ", Choice.EXCLUSIVE);
	private ChoiceGroup answer7 = new ChoiceGroup("Choose an answer: ", Choice.EXCLUSIVE);
	private ChoiceGroup answer8 = new ChoiceGroup("Choose an answer: ", Choice.EXCLUSIVE);
	private ChoiceGroup answer9 = new ChoiceGroup("Choose an answer: ", Choice.EXCLUSIVE);
	private ChoiceGroup answer10 = new ChoiceGroup("Choose an answer: ", Choice.EXCLUSIVE);
	
	
	public Trivia ()
	{
		try
		{
			background = Image.createImage("/trivia.png");
			menu.append (background);
			/*Alert wait = new Alert("Welcome",null,null,null);
			wait.setTimeout(5000);*/
			StringItem msg =  new StringItem ("Please select an option: ",null);
			menu.append(msg);
			
			options.append("New Game", null);
			options.append("High Scores",null);
			options.append("Help", null);
			choice = menu.append(options);			
			menu.addCommand(exit);
			//menu.addCommand(startNew);
			menu.addCommand(select);
				
			menu.setCommandListener(this);
			display.setCurrent(menu);
			
			
		}
		catch(Exception e)
		{
			System.err.println(e.getMessage());
		}
	}
	public void commandAction (Command c, Displayable d)
	{
		choice = options.getSelectedIndex();
		
		if (c == exit)
		{
			destroyApp(false);
			notifyDestroyed();
		}
		/*else if (c == startNew)
		{
					
			f = new Form("New Player");

			tempname = new TextField("Enter your Name:","", 255, TextField.ANY);
			f.append(tempname);								
			f.addCommand(exit);
			f.addCommand(submit);
			f.setCommandListener(this);					
			display.setCurrent(f);
		}*/
		else if (options.getString(choice) == "New Game")
		{
			choice = 0;
			category = new Form("Categories");		
			categories.append("Easy",null);
			categories.append("Normal",null);
			categories.append("Expert",null);
			
			difficulty = category.append(categories);
			
			category.addCommand(exit);
			category.addCommand(start);
			display.setCurrent(category);
			category.setCommandListener(this);
		}
		else
		if ( options.getString(choice) == "High Scores" || c==HScore )
		{
					
			get_scores();
		}
		else
		if ( options.getString(choice)== "Help" )
		{
			Help();
		}
		else if(c == start)
		{
			playername = tempname.getString();
			difficulty = categories.getSelectedIndex();
			if (life_count > 0)
			{
				//if (categories.getString (difficulty)== "Easy")
				//{
				
					question_count++;
					Game();
			}
			/*	else
				if (categories.getString(difficulty)== "Normal")
				{
					//next category
				}
				else
				{
					//last category
				}
				
			*/
			else
			if (c ==  Back)
			{
				//go to previous form
			}
			else
			{
				game_over = new Form ("Game Over");
				StringItem end_message = new StringItem ("Oh no! You have run out of lives. Thank you for playing.",null);
				game_over.append(end_message);
				game_over.addCommand(exit);
				//Your score was: 
				display.setCurrent(game_over);
				game_over.setCommandListener(this);
			}
		}
	}
	public void startApp()
	{
		
		display.setCurrent(menu);
	}
	public void pauseApp()
	{
	}
	public void destroyApp(boolean unconditional)
	{
		//notifyDestroyedApp();
	}
	
	public void store_score(String name, int score)//sorry this one should be called at the end of teh game ok
	{
		if(newscore(score)== true)
		{
			sort_scores(score);
			get_scores();
		}
		else
		{
			//your score did not make high score
		}
	}
	public void get_scores()
	{
		int i;
		StringItem success = new StringItem ("Highscores",null);
		HighScores = new Form ("High Scores");
		
		HighScores.addCommand(exit);// change this to a back command
		HighScores.setCommandListener(this);
		display.setCurrent(HighScores);
		
		for (i=0; i <5; i++)
		{
			
				//playerscore[i] = new StringItem(" ","");//wa dis????
				//playerscore[i].setText(""+(tempscore)); // what is this line saying its assigning the score to the variable...i had problems adding an int
				
			
		}
	}
	public boolean newscore(int s) 
	{
		int i;
		for (i = 0; i <5;i++)
		{
			if (score[i] < s)
			{
				return true;
			}
		}
		return false;
	}
	public void sort_scores(int s)
	{
		int temp;
		String temp2; 
		int i,z;
		int score_array[]={0};//isnt this how u declare arrays? yea?
		for (i=0;i<6; i++)
		{
			if (i == 4)
			{
				score_array[i] = s;
			}
			else
			{
				score_array[i] = score[i];
			}
		}
		
		for (i =0;i<6;i++)
		{
			for (z=0;z<6;z++)
			{
				//dis is bubble sort right?
				if (score_array[z] < score_array[z+1])
				{
					temp = score_array[z];
					score_array[z]= score_array[z+1];
					score_array[z+1]=temp;
					temp2 = name[z];
					name[z] = name[z+1];
					name[z+1] = temp2;
				}
			}//supm likea somhmm
		}
	}
	
	
	public void Game ()
	{
		if (question_count == 1)
		{
			question_form = new Form ("Trivia");
			StringItem question1 = new StringItem("Question 1: In the game of Football, how many players are on a Team?",null);

			question_form.append (question1);                              
			answer.append("9", null);
			answer.append("10",null);
			answer.append("11", null);
			answer.append("12",null);
			
			
			ans = question_form.append(answer);
			
			question_form.addCommand(exit);
			question_form.addCommand(select);
			question_form.setCommandListener(this);
			display.setCurrent(question_form);
		}
		else if (question_count ==2)
		{
			ans = answer.getSelectedIndex();
			if (answer.getString(ans) =="11")
			{
				tempscore = tempscore + 200;
				question_form2 = new Form ("Trivia");
				StringItem question2 = new StringItem ("Question 2: The person in charge of a foot ball match is called?",null);
				question_form2.append(question2);
				answer2.append("Umpire", null);
				answer2.append("Goal Keeper",null);
				answer2.append("Judge", null);
				answer2.append("Referee",null);
				
				ans2 = question_form2.append(answer2); 
				
				question_form2.addCommand(exit);
				question_form2.addCommand(select);
				question_form2.setCommandListener(this);
				display.setCurrent(question_form2);
			}
			else
			{
				Alert wrong_answer = new Alert("Welcome","Incorrect answer: <put life count here>",null,null);
				wrong_answer.setTimeout(5000);// alert nt workin.... hmmm 
				question_count--;
				life_count --;
			}
		}
		else if (question_count == 3)
		{
			ans2 = answer2.getSelectedIndex();
			if (answer2.getString(ans2) == "Referee")
			{
				tempscore = tempscore + 200;
				question_form3 = new Form ("Trivia");
				StringItem question3 = new StringItem ("Question 3: What year did Christopher Columbus discover the Caribbean?",null);
				question_form3.append(question3);
				answer3.append("1492", null); //answer
				answer3.append("1494",null);
				answer3.append("1501", null);
				answer3.append("1499",null);
				//seems legit yup..jst wondering why it printing the stuff above the ques...its the code we put in to check the index., .ya
				ans3 = question_form3.append(answer3);
				//brb
				question_form3.addCommand(exit);
				question_form3.addCommand(select);
				question_form3.setCommandListener(this);
				display.setCurrent(question_form3);
			}
			else
			{
				
				Alert wrong_answer = new Alert("Welcome","Incorrect answer: <put life count here>",null,null);
				wrong_answer.setTimeout(5000);
				question_count--;
				life_count --;
			}
		}
		else if (question_count == 4)
		{
		
			ans3 = answer3.getSelectedIndex();
			if (answer3.getString(ans3)== "1492")
			{
				tempscore = tempscore + 200;
				question_form4 = new Form ("Trivia");
				StringItem question4 = new StringItem ("Question 4: Plants receive their nutrients mainly from?",null);
				question_form4.append(question4);
				answer4.append("chlorophyll", null); 
				answer4.append("Soil",null); //answer
				answer4.append("Light", null);
				answer4.append("Atmosphere",null);
				
				ans4 = question_form4.append(answer4);
				
				question_form4.addCommand(exit);
				question_form4.addCommand(select);
				question_form4.setCommandListener(this);
				display.setCurrent(question_form4);
			}
			else
			{
				Alert wrong_answer = new Alert("Welcome","Incorrect answer: <put life count here>",null,null);
				wrong_answer.setTimeout(5000);
				question_count--;
				life_count --;
			}
		}
		else if (question_count == 5)
		{
			ans4 = answer4.getSelectedIndex();
			if (answer4.getString(ans4)== "Soil")
			{
				tempscore = tempscore + 200;
				question_form5 = new Form ("Trivia");
				StringItem question5 = new StringItem ("Question 5: If it takes 3 monkeys 3 minutess to eat 3 bananas, how long would it take 1 monkey?",null);
				question_form5.append(question5);
				answer5.append("1 minute", null); 
				answer5.append("2 minutes",null);
				answer5.append("6 minutes", null);
				answer5.append("3 minutes",null);//answer
				
				ans5 = question_form5.append(answer5);
				
				question_form5.addCommand(exit);
				question_form5.addCommand(select);
				question_form5.setCommandListener(this);
				display.setCurrent(question_form5);
			}
			else
			{
				Alert wrong_answer = new Alert("Welcome","Incorrect answer: <put life count here>",null,null);
				wrong_answer.setTimeout(5000);
				question_count--;
				life_count --;
			}
		}
		else if (question_count == 6)
		{
			ans5 = answer5.getSelectedIndex();
			if (answer5.getString(ans5)== "3 minutes")
			{
				tempscore = tempscore + 200;
				question_form6 = new Form ("Trivia");
				StringItem question6 = new StringItem ("Question 6: The largest Gulf in the World is?",null);
				question_form6.append(question6);
				answer6.append("Gulf of Carpentaria", null); 
				answer6.append("Persian Gulf",null);
				answer6.append("Gulf of Mexico", null);//answer;
				answer6.append("Gulf of Mannar",null);
				
				ans6 = question_form6.append(answer6);
				
				question_form6.addCommand(exit);
				question_form6.addCommand(select);
				question_form6.setCommandListener(this);
				display.setCurrent(question_form6);
			}
			else
			{
				Alert wrong_answer = new Alert("Welcome","Incorrect answer: <put life count here>",null,null);
				wrong_answer.setTimeout(5000);
				question_count--;
				life_count --;
			}
		}
		else if (question_count == 7)
		{
			ans6 = answer6.getSelectedIndex();
			if (answer6.getString(ans6)== "Gulf of Mexico")
			{
				tempscore = tempscore + 200;
				question_form7 = new Form ("Trivia");
				StringItem question7 = new StringItem ("Question 7: Who was the first black President of the United States of America",null);
				question_form7.append(question7);
				answer7.append("Bill Clinton", null); 
				answer7.append("George Bush",null);
				answer7.append("Martin Luther", null);
				answer7.append("Barrack Obama",null);//answer
				
				ans7 = question_form7.append(answer7);
				
				question_form7.addCommand(exit);
				question_form7.addCommand(select);
				question_form7.setCommandListener(this);
				display.setCurrent(question_form7);
			}
			else
			{
				Alert wrong_answer = new Alert("Welcome","Incorrect answer: <put life count here>",null,null);
				wrong_answer.setTimeout(5000);
				question_count--;
				life_count --;
			}
		}
		else if (question_count == 8)
		{
			ans7 = answer7.getSelectedIndex();
			if (answer7.getString(ans7)== "Barrack Obama")
			{
				tempscore = tempscore + 200;
				question_form8 = new Form ("Trivia");
				StringItem question8 = new StringItem ("Question 8: Which system is responsible for the flow of blood in the body?",null);
				question_form8.append(question8);
				answer8.append("Endocrine System", null); 
				answer8.append("Immune System",null);
				answer8.append("Circulatory System", null);//answer
				answer8.append("Nervous System",null);
				
				ans8 = question_form8.append(answer8);
				
				question_form8.addCommand(exit);
				question_form8.addCommand(select);
				question_form8.setCommandListener(this);
				display.setCurrent(question_form8);
			}
			else
			{
				Alert wrong_answer = new Alert("Welcome","Incorrect answer: <put life count here>",null,null);
				wrong_answer.setTimeout(5000);
				question_count--;
				life_count --;
			}
		}
		else if (question_count == 9)
		{
			ans8 = answer8.getSelectedIndex();
			if (answer8.getString(ans8)== "Circulatory System")
			{
				tempscore = tempscore + 200;
				question_form9 = new Form ("Trivia");
				StringItem question9 = new StringItem ("Question 9: What does the Italian musical term adagio mean?",null);
				question_form9.append(question9);
				answer9.append("Slow", null);//answer 
				answer9.append("Smooth",null);
				answer9.append("Fast", null);
				answer9.append("Loud",null);
				
				ans9 = question_form9.append(answer9);
				
				question_form9.addCommand(exit);
				question_form9.addCommand(select);
				question_form9.setCommandListener(this);
				display.setCurrent(question_form9);
			}
			else
			{
				Alert wrong_answer = new Alert("Welcome","Incorrect answer: <put life count here>",null,null);
				wrong_answer.setTimeout(5000);
				question_count--;
				life_count --;
			}
		}
		else if (question_count == 10)
		{
			ans9 = answer9.getSelectedIndex();
			if (answer9.getString(ans9)== "Slow")
			{
				tempscore = tempscore + 200;
				question_form10 = new Form ("Trivia");
				StringItem question10 = new StringItem ("Question 10: Which country is Usain Bolt from?",null);
				question_form10.append(question10);
				answer10.append("Uganda", null);
				answer10.append("Jamaica",null);//answer 
				answer10.append("United States of America", null);
				answer10.append("Trelawny",null);
				
				ans10 = question_form10.append(answer10);
				
				question_form10.addCommand(exit);
				question_form10.addCommand(select);
				question_form10.setCommandListener(this);
				display.setCurrent(question_form10);
			}
			else
			{
				Alert wrong_answer = new Alert("Welcome","Incorrect answer: <put life count here>",null,null);
				wrong_answer.setTimeout(5000);
				question_count--;
				life_count --;
			}
		}
		else if (question_count == 11)
		{
			ans10 = answer10.getSelectedIndex();
			if (answer10.getString(ans10)== "Jamaica")
			{
				tempscore = tempscore + 200;
				Congratulation = new Form ("Trivia");
				StringItem success = new StringItem ("You Have Won!, Congrats!?",null);
				StringItem tscore = new StringItem(" ","");
				tscore.setText(""+(tempscore));
				
				Congratulation.append(success);
				
				Congratulation.append("\n Your Score is:");
				Congratulation.append(tscore);
				Congratulation.append(playername);
				Congratulation.addCommand(exit);
				Congratulation.addCommand(HScore);
				Congratulation.setCommandListener(this);
				display.setCurrent(Congratulation);
				store_score(playername, tempscore);
				
				
	
			}
			else
			{
				Alert wrong_answer = new Alert("Welcome","Incorrect answer: <put life count here>",null,null);
				wrong_answer.setTimeout(5000);
				question_count--;
				life_count --;
			}
		}
		
	}
	
	public void Help()
	{
		Form form = new Form("How to play");
		Command back = new Command("Back",Command.BACK,2);
		String string = file();
		if (string != null)
		{
			StringItem info = new StringItem(string,null);
			form.append(info);
			form.addCommand(back);
			form.setCommandListener(this);
			display.setCurrent(form);
		}
	}
	
	private String file()
	{
		InputStream is = getClass().getResourceAsStream("/help.txt");
		StringBuffer sb = new StringBuffer();
		try
		{
			int chars, i = 0;
			while ((chars = is.read()) != -1)
			{
				sb.append((char) chars);
			}
			return sb.toString();
		}
		catch (Exception e)
		{
		}
		return null;
	}
	
	
	
}